#!/usr/bin/env python
#coding:utf-8

import os
import sys
import re

CONFILE = 'list.conf'

class setupUbuntu:
    '''程序主类'''
    def __init__(self, conf):
        '''类的初始化，获取配置文件名称'''
        self.conf = conf
        #self.ok = True

    def readConf(self):
        '''读取配置文件列表，并返回软件名称'''
        result = []
        try:
            f = open(self.conf, 'r')
        except IOError:
            print '##list.conf 配置文件没找到...'
            sys.exit()
            
        for line in f.readlines():
            line = line.strip()
            if not len(line) or line.startswith('#'):
                continue
            result.append(line)
        return result

    def printMenu(self):
        '''打印需安装的软件列表'''
        print 'SoftWare List:\n'
        lists = self.readConf()
        for l in lists:
            name = l.split()
            print '[%s] - %s' % (name[0], name[1])
            
    def softList(self):
        lists = self.readConf()
        while True:
            self.printMenu()
            c = raw_input('enter you choice(q exit):')
            if c == 'q':
                sys.exit()  #退出程序
            else:
                try:
                    int(c)
                except ValueError:
                    print '非法字符，请重新输入'
                    continue
                
            if int(c) not in range(len(lists)):#输入除q外的在于列表数量的数字
                print '选择不在范围内，请重新输入'
                continue
            else:
                softName = lists[int(c)].split()[1]
                return softName
    
    def run(self, softname):
        '''执行安装操作'''
        #cmd = 'apt-get install %s -y' % softname
        os.system(softname)

    def usrage(self):
        '''显示程序相关信息及其说明'''
        pass

def main():
    app = setupUbuntu(CONFILE)
    while True:
        softname = app.softList()
        app.run(softname)

if __name__ == '__main__':
    main()
